package cn.nbxz.dao;

import cn.nbxz.domain.Customer;
import cn.nbxz.domain.Order;
import cn.nbxz.util.DateHelper;
import cn.nbxz.util.JdbcHelper;


import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

public class OrderDao implements Dao<Order,String> {
    private String table=" t_orders ";
    @Override
    public Order load(String id) {
        String SQL="select id,name,address,telephone,create_time,total,customer_id from"
                +table +" where id=?";
        JdbcHelper helper = new JdbcHelper();
        List<Map<String,Object>> list = helper.query( SQL , id ) ;
        helper.destory();
        if( list != null && list.size() == 1 ) {
            Map<String,Object> map = list.get( 0 );
            Order o =   this.cast( map );
            return o ;
        }
        return null;
    }

    @Override
    public List<Order> list() {
        String SQL="select id,name,address,telephone,create_time,total,customer_id from "
                +table ;
        JdbcHelper helper = new JdbcHelper();
        List<Map<String,Object>> list = helper.query( SQL) ;

        helper.destory();
        if( list != null && list.size()> 0 ) {
            List<Order> orders = new ArrayList<>();
            for( int i = 0 ; i < list.size() ; i++ ) {
                Map<String, Object> map = list.get(i);

                Order o =   this.cast( map );
                orders.add(o);
            }
            return orders;
        }
        return null;
    }

    @Override
    public Order cast(Map<String, Object> map) {

        Order order=new Order();
        order.setId((String)map.get("id"));
        order.setName((String)map.get("name"));
        order.setAddress((String)map.get("address"));
        order.setTelephone((String)map.get("telephone"));
        order.setCreateTime((Date) map.get("create_time"));
        order.setTotal((double)map.get("total"));
        Customer customer= new CustomerDao().load((Integer)map.get("customer_id"));
        order.setCustomer(customer);

        return order;
    }

    @Override
    public boolean  save(Order o) {
        return true;
    }
    public int   save1(Order o) {
        String SQL="insert into "+table+" (id,name,address,telephone,create_time,total,customer_id )"
                +" values(?,?,?,?,?,?,?) ";
        java.sql.Date create_time= DateHelper.cast( o.getCreateTime() );
        Integer customer_id= o.getCustomer().getId();
        Object[] array={o.getId(),o.getName(),o.getAddress(),o.getTelephone(),create_time,o.getTotal(),customer_id};
        JdbcHelper helper = new JdbcHelper();
        // 执行插入语句，并获得由数据库产生的主键值
       int n = helper.update( SQL , array );
        //helper.update(SQL,array);
        //Order id ??????
        helper.destory();
        // 如果 id 不等于 -1  则视为保存成功( 在 JdbcHelper 的 insert 方法中有约定 )
        return n;

    }

    @Override
    public boolean update(Order o) {
        String SQL="update "+table +"set name=?, address=?, telephone=?, total=?, customer_id=? "
                +" where id=?";
        Integer customer_id= o.getCustomer().getId();

        Object[] array={o.getName(),o.getAddress(),o.getTelephone(),o.getTotal(),customer_id,o.getId()};
        JdbcHelper helper = new JdbcHelper();
        int count = helper.update( SQL , array );
        helper.destory();
        return count == 1 ;

    }

    @Override
    public boolean delete(String id) {
        String SQL = "DELETE FROM " + table + " WHERE id = ? ";
        JdbcHelper helper = new JdbcHelper();
        int count = helper.update( SQL , id );
        helper.destory();
        return count == 1 ;

    }
}
